package com.maddox.relax.view;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import androidx.fragment.app.FragmentActivity;


import com.maddox.relax.PhotoPicker;
import com.maddox.relax.R;
import com.maddox.relax.listener.OnImageTakeCompleteListener;
import com.maddox.relax.model.ImageSet;

import java.io.File;

/**
 * @author qingli
 * 拍照或者选择相册弹框
 */
public class TakePhotoWindow extends Dialog {

    private FragmentActivity activity;
    private OnImageTakeCompleteListener imageTakeCompleteListener;
    private int mimeType = ImageSet.TYPE_ALL_IMAGE;
    private boolean isMultiple =true;


    public TakePhotoWindow(@NonNull FragmentActivity context) {
        this(context, R.style.TakePhotoStyle);
        this.activity = context;
    }

    public TakePhotoWindow(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_take_photo_window);
        setCanceledOnTouchOutside(false);
        setCancelable(true);
        Window window = getWindow();
        assert window != null;
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = (int) (getScreenWidth() * 0.95);
        layoutParams.y = 20;
        window.setAttributes(layoutParams);
        View btnTakePhoto = findViewById(R.id.take_photo);
        View btnTakePicture = findViewById(R.id.take_picture);
        View btnTakeCancle = findViewById(R.id.take_cancle);
        btnTakePhoto.setOnClickListener(v -> {
            startPhoto(activity, imageTakeCompleteListener);
            this.dismiss();
        });
        btnTakePicture.setOnClickListener(v -> {
            startPickPicture(activity, imageTakeCompleteListener);
            this.dismiss();
        });
        btnTakeCancle.setOnClickListener(v -> {
            this.dismiss();
        });

    }

    /**
     * 拍照或者选择照片的监听器
     *
     * @param listener 必须设置
     */
    public void setImageTakeCompleteListener(OnImageTakeCompleteListener listener) {
        this.imageTakeCompleteListener = listener;
    }


    /**
     * {@link ImageSet#TYPE_ALL_MEDIA } 所有媒体
     * {@link ImageSet#TYPE_ALL_IMAGE } 所有图片
     * {@link ImageSet#TYPE_ALL_VIDEO } 所有视频
     *
     * @param mimeType 选择照片的类型
     */
    public void setImageTakeMimeType(int mimeType) {
        this.mimeType = mimeType;
    }

    /**
     * @param isMultiple 是否开启多选
     */
    public void setImageTakeMultiple(boolean isMultiple) {
        this.isMultiple = isMultiple;
    }


    /**
     * 选取相册的照片
     * 分为多选和单选功能 默认多选 图片文件
     */
    private void startPickPicture(FragmentActivity activity, OnImageTakeCompleteListener listener) {
        startPickPicture(activity, mimeType, isMultiple, listener);
    }


    /**
     * @param activity
     * @param mimeType   文件类型 {@link ImageSet#id }
     * @param isMultiple
     * @param listener
     */
    private void startPickPicture(FragmentActivity activity, int mimeType, boolean isMultiple, OnImageTakeCompleteListener listener) {
        PhotoPicker.takePhotoAlbum(activity,mimeType,isMultiple,listener);
    }


    /**
     * 调用拍照功能 ,并自动压缩返回路径
     *
     * @param activity 必须为AppCompatActivity 类型
     * @param listener 照片返回的监听
     */
    private void startPhoto(FragmentActivity activity, OnImageTakeCompleteListener listener) {
        PhotoPicker.takePhoto(activity, listener);
    }

    //获取屏幕宽度
    private int getScreenWidth() {
        int width = activity.getResources().getDisplayMetrics().widthPixels;
        return width;
    }
}
